package ModelLayer;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Iterator;

public class ContractorRecords {
	
	private ArrayList<Contractor> contractors;
	private static ContractorRecords instance = null;

	private ContractorRecords() {
		contractors = new ArrayList<Contractor>();
	}
	
	public static ContractorRecords getInstance()
    {
        if(instance == null)
        {
            instance = new ContractorRecords();
        }
        return instance;
    }	
	
	public Contractor getContractor(int id) throws RealException {		
		Contractor theContractor = null;
        boolean found = false;
        Iterator<Contractor> it = contractors.iterator();
        while(it.hasNext() && !found)
        {
            Contractor contractor = it.next();
            if(contractor.getId() == id)
            {
                theContractor = contractor;
                found = true;
            }	
        }
        if(!found) throw new RealException("Contractor was not found");
        return theContractor;
	}
	
	public ArrayList<Contractor> getContractors() {
		return contractors;
	}
	
	public void removeContractor(int id) throws RealException
    {        
        Contractor contractor = getContractor(id);
        if(contractor != null)
        {
            contractors.remove(contractor);
        }
    }
	
	public void updateContractor(int id ,int choise, String newValue) throws RealException
	{
		Contractor contractor=getContractor(id);
		if(contractor!=null)
		{
			switch(choise)
			{
				case 1:
				{
					contractor.setName(newValue);
					break;
				}
				case 2:
				{
					contractor.setAddress(newValue);
					break;
				}
				case 3:
				{
					contractor.setPhone(Long.parseLong(newValue));
					break;
				}
			}	
		}
	}

    public void addContractor(String name, String address, long phone)
    {   
    	Contractor contractor = new Contractor(name, address, phone);
        contractors.add(contractor);
    }
    
    public String printContractor(int id) throws RealException {
		return getContractor(id).toString();
	}

    public ArrayList<String> getAllContractors() {
		ArrayList<String> texts = new ArrayList<String>();
		for (Contractor contractor : contractors) {
			texts.add(contractor.toString());
		}
		return texts;
	}
    
    public void checkContractor(int id) throws RealException {		
        boolean found = false;
        Iterator<Contractor> it = contractors.iterator();
        while(it.hasNext() && !found)
        {
            Contractor contractor = it.next();
            if(contractor.getId() == id)
            {
                found = true;
            }	
        }
        if(!found) throw new RealException("Contractor was not found");
	}

	@SuppressWarnings("unchecked")
	public void load(ObjectInputStream obj_in) throws IOException, ClassNotFoundException
	{
		this.contractors = (ArrayList<Contractor>) obj_in.readObject();
		Contractor.setLastId(((Integer)obj_in.readObject()).intValue());
	}
	
	public void save(ObjectOutputStream obj_out) throws IOException
	{		
		obj_out.writeObject(contractors);
		obj_out.writeObject(new Integer(Contractor.getLastId()));
	}

}
